package com.hwhp.service.console.impl;

import java.util.ArrayList;
import java.util.List;

import com.hwhp.dao.HibernateBaseDao;
import com.hwhp.dao.HibernateUtil;
import com.hwhp.entity.Menu;
import com.hwhp.service.console.MenuService;



/**
 * @author zyc Jun 25, 2009 desc: curd action
 */
public class MenuServiceImpl implements MenuService {
	private HibernateBaseDao baseDao = null ;
	/**
	 * 获取菜单对象
	 * 
	 * @param menuId
	 * @return
	 */
	public  Menu getMenuById(String menuId) {
		Menu menu = null;
		if (menuId != null && !"".equals(menuId)) {
			//menu = (Menu) HibernateUtil.get(Menu.class, menuId);
			menu = (Menu) baseDao.get(Menu.class, menuId);
		}
		return menu;
	}

	/**
	 * 根据id获取下级菜单对象列表
	 * 
	 * @param menuId
	 * @param isused
	 * @return
	 */
	public  List<Menu> getSubMenuById(String menuId, String isused) {
		List<Menu> menuList = null;
		if (menuId != null && !"".equals(menuId)) {
			String hql = "from Menu as m where m.parentid='" + menuId + "' ";
			if (isused != null && !"".equals(isused)) {
				hql = hql + "and m.isused=" + isused;

			}
			hql = hql + " order by m.code";
			//menuList = (List<Menu>) HibernateUtil.query(hql);
			menuList = (List<Menu>) baseDao.query(hql);
		}
		return menuList;
	}

	/**
	 * 
	 * 
	 * @param menuId
	 * @param isused
	 * @return
	 */
	public  List<Menu> getAllSubMenuById(String menuId, String isused) {
		List<Menu> menuList = null;
		if (menuId != null && !"".equals(menuId)) {
			String hql = "from Menu as m where m.parentid='" + menuId + "' ";
			if (isused != null && !"".equals(isused)) {
				hql = hql + "and m.isused=" + isused;

			}
			hql = hql + " order by m.code";
			menuList = (List<Menu>) HibernateUtil.query(hql);
			
			//menuList = (List<Menu>) baseDao.query(hql);
		}
		if (menuList != null && menuList.size() > 0) {
			for (int i=0;i<menuList.size();i++) {
				Menu menu=menuList.get(i);
				List<Menu> menuSubList = getAllSubMenuById(menu.getId(),isused);
				if (menuSubList != null && menuSubList.size() > 0) {
				for (int j=0;j<menuSubList.size();j++) {
					Menu submenu=menuSubList.get(j);
					menuList.add(submenu);
				}
				}
			}

		}
		
		return menuList;
	}
	/**
	 * 根据id集获取菜单列衄1�7
	 * @param menuIdSet
	 * @return
	 */
	public  List<Menu> getAllMenuByIdSet(String menuIdSet){
		List<Menu> menuList = null;
		if (menuIdSet != null && !"".equals(menuIdSet)) {
			String hql = "from Menu as m where m.isused=1 and m.id in (" + menuIdSet + ") and m.isused=1";
			hql = hql + " order by m.code";
			//menuList = (List<Menu>) HibernateUtil.query(hql);
			menuList = (List<Menu>) baseDao.query(hql);
		}
		return menuList;
	}
	/**
	 * 根据id集获取菜单名称集各
	 * @param menuIdSet
	 * @return
	 */
	public  String getAllMenuNameByIdSet(String menuIdSet){
		String nameSet="";
		List<Menu> menuList =  getAllMenuByIdSet(menuIdSet);
		//List<Menu> menuList = (List<Menu>) baseDao.query(hql);
		for(Menu menu:menuList){
			nameSet=nameSet+","+menu.getName();
		}
		if(nameSet!=null&&!"".equals(nameSet)){
			nameSet=nameSet.substring(1);
		}
		return nameSet;
	}
	
	/**
	 * 获取上级对象列表
	 * @param menuId
	 * @param isused
	 * @return
	 */
	public  List<Menu> getAllFatherMenuById(String menuId, String isused) {
		List<Menu> menuList = new ArrayList();
		if (menuId != null && !"".equals(menuId)&&!"-1".equals(menuId)) {
			Menu currentMenu=new Menu();
			currentMenu=getMenuById(menuId);
			menuList.add(currentMenu);
			String parentid=currentMenu.getParentid();
			List<Menu> menuSubList = getAllFatherMenuById(parentid,isused);
			if (menuSubList != null && menuSubList.size() > 0) {
				for (int j=0;j<menuSubList.size();j++) {
					Menu submenu = menuSubList.get(j);
					menuList.add(submenu);
				}
				}
		}
		return menuList;
	}
	/**
	 * 根据id删除菜单对象ݲ˵�idɾ��˵ￄ1�7
	 * 
	 * @param menuId
	 * @return
	 */
	public  boolean deleteMenuById(String menuId) {

		boolean success = true;
		if (menuId != null && !"".equals(menuId)) {
			//Menu menu = (Menu) HibernateUtil.get(Menu.class, menuId);
			Menu menu = (Menu) baseDao.get(Menu.class, menuId);
			//success = HibernateUtil.delete(menu);
			try{
				 baseDao.delete(menu);
				 success = true ;
			}catch(Exception e){
				success = false ;
				
			}
		} else {
			success = false;
		}
		return success;
	}

	/**
	 * 删除扄1�7有下级菜单ݲ˵�idɾ���¼��˵�id
	 * 
	 * @param menuId
	 * @return
	 */
	public  boolean deleteSubMenuById(String menuId) {
		boolean success = true;
		if (menuId != null && !"".equals(menuId)) {
			List<Menu> subList = getSubMenuById(menuId, "");
			for (Menu menu : subList) {
				//success = HibernateUtil.delete(menu);
				try{
					 baseDao.delete(menu);
					 success = true ;
				}catch(Exception e){
					success = false ;
					
				}
			}
		} else {
			success = false;
		}
		return success;

	}

	/**
	 * 新增或修改菜单对豄1�7
	 * 
	 * @param menu
	 * @return
	 */
	public  Menu saveOrUpdateMenu(Menu menu) {
		boolean success = true;
		if (menu != null) {
			String menuId = menu.getId();
			if (menuId != null && !"".equals(menuId)) {
				//success = HibernateUtil.update(menu);
				try{
					baseDao.update(menu);
					success = true ;
				}catch(Exception e){
					success = false ;
					e.printStackTrace();
				}
				
			} else {
				//HibernateUtil.save(menu);
				try {
					baseDao.save(menu);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} else {
			return null;
		}
		return menu;
	}

	public void setBaseDao(HibernateBaseDao baseDao) {
		this.baseDao = baseDao;
	}

	public HibernateBaseDao getBaseDao() {
		return baseDao;
	}
}
